<?php

/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2006-11-06 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$myMoney = new Money;
class Money
{
	// TODO configurable para, used later
	var $pay_back_url = 'http://wjl.cn/money/returl.php';
	
	function Money()
	{		
		global $BIZ,$INCOME,$DB,$feedb,$CONF;
		
		$DB -> db_query("SELECT value FROM biz_global_configure WHERE `key`='account_info'");
		$rs = $DB -> db_fetch_row();
		$account_info= unserialize($rs['value']);
		
//		echo "<pre>";
//		print_r($INCOME);
//		print_r($BIZ);
//		print_r($account_info);
//		echo $BIZ->nav_link[0][0];
//		echo "</pre>";
		
		if ( empty($BIZ->seller['seller_id']) or $BIZ->seller['seller_id'] == "" or $BIZ->seller['seller_id'] == 0 )
		{
			$BIZ->error( "用户没有登陆，不能使用此功能","?act=Login&code=00" );
		}
		switch($INCOME[code])
		{
			case '1':
				/**
				 * @category cncard.net interface
				 * @Description: 云网网关接口
				 * @Copyright (c) 北京云网
				 * @version 2.0
				 */

				if($account_info['cncard'][0] = '0')
					break;
				$my_order_num = $this->createOrderNum();
				$orderDay=date("Ymd");
				// configurable para
				$c_signstr=md5($account_info['cncard'][1]
					.$my_order_num
					.$_POST[c_orderamount]
					.$orderDay
					.'0'.'1'
					.$this->pay_back_url
					.'01'
					.'0001'
					.'0'
					.$account_info['cncard'][2]);

				$BIZ ->assign("c_mid",$account_info['cncard'][1]);
				// end configurable
				
				$BIZ ->assign("step",'1');
				$BIZ ->assign("c_order", $my_order_num);
				$BIZ ->assign("c_name", $_POST[c_name]);
				$BIZ ->assign("c_address", $_POST[c_address]);
				$BIZ ->assign("c_tel", $_POST[c_tel]);
				$BIZ ->assign("c_post", $_POST[c_post]);
				$BIZ ->assign("c_email", $_POST[c_email]);
				$BIZ ->assign("c_orderamount", $_POST[c_orderamount]);
				$BIZ ->assign("orderDay", $orderDay);
				$BIZ ->assign("c_signstr", $c_signstr);
			    break;
			case '2':
				/**
				 * @category 99bill.com interface
				 * @Description: 快钱网关接口
				 * @Copyright (c) 上海快钱信息服务有限公司
				 * @version 1.0
				 */
				if($account_info['bill'][0] = '0')
					break;
				// configurable para
				$merchant_id 	= $account_info['bill'][1];		///商户编号
				$merchant_key 	= $account_info['bill'][2];		///商户密钥
				$orderid 		= $this->createOrderNum();		///订单编号
				$amount 		= $_POST[c_orderamount];		///订单金额
				$curr 			= "1";							///货币类型,1为人民币
				$isSupportDES 	= "2";							///是否安全校验,2为必校验,推荐
				
				$merchant_url 	= $this->pay_back_url;			///支付结果返回地址
				$pname 			= $_POST[c_name];				///支付人姓名
				$commodity_info = $_POST[c_address];			///商品信息
				$merchant_param = $_POST[c_email];				///商户私有参数
					
				$pemail="";		///传递email到快钱网关页面
				$pid="";		///代理/合作伙伴商户编号
				   
				///生成加密串
				$ScrtStr="merchant_id=".$merchant_id."&orderid=".$orderid."&amount=".$amount."&merchant_url=".$merchant_url."&merchant_key=".$merchant_key;
				$mac = strtoupper(md5($ScrtStr)); 
	
				$BIZ ->assign("step",'2');
				$BIZ ->assign("orderid", $orderid);
				$BIZ ->assign("pname", $pname);
				$BIZ ->assign("commodity_info", $commodity_info);
				$BIZ ->assign("amount", $amount);
				$BIZ ->assign("mac", $mac);
				
				$BIZ ->assign("merchant_id", $merchant_id);
				$BIZ ->assign("currency", $curr);
				$BIZ ->assign("isSupportDES", $isSupportDES);
				$BIZ ->assign("merchant_url", $merchant_url);
				$BIZ ->assign("merchant_param", $merchant_param);
				$BIZ ->assign("pemail", $pemail);				
				$BIZ ->assign("pid", $pid);
																			   
			    break;
			case '3':
				/**
				 * @category https://www.alipay.com/ interface
				 * @Description: 支付宝接口
				 * @Copyright (c) 杭州阿里
				 * @version 1.0
				 */			
				require ROOT_PATH.'source/libs/Alipay_Payto.php';				
				if($account_info['alipay'][0] = '0')
					break;
				$interfaceurl	=	"https://www.alipay.com/payto:";
				$sitename		=	$BIZ->nav_link[0][0];
				$weburl			=	"http://wjl.cn";
				
				$seller			=	$account_info['alipay'][1];								//支付宝账号
				$payalikey		=	$account_info['alipay'][2];								//安全校验码
				$imgurl			=	"images/alipay_bwrx.gif";	//按钮图片源
				$imgtitle		=	"使用支付宝购买";											//按钮图片说明
				
				$cmd			=	'0001';
				$subject		=	'便利店充值';
				$body			=	'充值人民币与本站点数对应比例为：1：10';
				$order_no		=	$this->createOrderNum();
				$price			=	$_POST[c_orderamount];
				$url			=	'';
				$type			=	'1';
				$number			= 	'1';
				$transport		=	'2';
				$ordinary_fee	=	"0";											//平邮费
				$express_fee	=	"0";											//快递费
				$readonly		=	'true';
				$buyer_msg		=	$_POST[c_address];
				$buyer			=	'';									//买家支付宝账号
				$buyer_name		=	$_POST[c_realname];
				$buyer_address	=	$_POST[c_name];
				$buyer_zipcode	=	$_POST[c_post];
				$buyer_tel		=	$_POST[c_tel];
				$buyer_mobile	=	'';
				$partner		=	'';
				
				$geturl	= new alipay_payto;
				$link	= $geturl->geturl
					(
					$cmd,$subject,$body,$order_no,$price,$url,$type,$number,$transport,
					$ordinary_fee,$express_fee,$readonly,$buyer_msg,$seller,$buyer,
					$buyer_name,$buyer_address,$buyer_zipcode,$buyer_tel,$buyer_mobile,$partner,
					$interfaceurl,$payalikey
					);
				
				$BIZ ->assign("step",'3');
				$BIZ ->assign("orderid", $order_no);
				$BIZ ->assign("pname", $buyer);
				$BIZ ->assign("buyer_name", $buyer_name);
				$BIZ ->assign("commodity_info", $buyer_msg);
				$BIZ ->assign("amount", $price);

				$BIZ ->assign("link", $link);
			default:
				break;
		}
		$BIZ ->assign("is99bill", $account_info['bill'][0]);
		$BIZ ->assign("isalipay", $account_info['alipay'][0]);		
		$BIZ ->assign("iscncard", $account_info['cncard'][0]);
		$BIZ ->assign("trader",$BIZ->nav_link[0][0]);
		$BIZ ->assign("seller",$BIZ->seller);
		$BIZ ->assign("rate",$feedb['point_rate']);
		$BIZ ->assign("telephone",$CONF['service_telephone']);
		$BIZ -> output("Money.tpl");
	}
	
	// private function
	// TODO add local datebase order management functionality
	function createOrderNum(){
		$a=time();
		$b=rand(1000,9999);
		$c=date("Ymd");
		return $c_order=$a.$b.$c;
	}
}
?>